"use strict";
cc._RF.push(module, 'dc2dbogMJdFHqB0rXK0Wl3w', 'StraightProp');
// script/StraightProp.js

"use strict";

/**
 * 直行道具
*/
cc.Class({
    extends: cc.Component,

    properties: {
        boostNode: cc.Node
    },

    onLoad: function onLoad() {
        this.initPublicData();
    },


    /**
     * 拿到人物节点
     */
    initPublicData: function initPublicData() {
        var can = cc.find("Canvas");
        this.player = can.getChildByName("player");
        this.animation = this.player.getComponent(cc.Animation);
        this.rigidBody = this.player.getComponent(cc.RigidBody);
        this.propRigidBody = this.node.getComponent(cc.RigidBody);
        // this.physicsBoxCollider = this.node.getComponent(cc.PhysicsBoxCollider);
        // this.heroBoxHeight = this.physicsBoxCollider.size.height;
    },

    /** 
     * 直行道具与人物碰撞之后的逻辑
    */
    onBeginContact: function onBeginContact(contact, selfCollider, otherCollider) {
        console.log("xiaowu");
        if (selfCollider.tag === cc.vv.TAG.ROCKET) {
            this.player.prop = "Rocket";
        } else if (selfCollider.tag === cc.vv.TAG.MOTO) {
            this.player.prop = "Moto";
        }
        this.propAction(this.player.prop);
    },
    /**
     * 具体状态的执行逻辑
     */
    propAction: function propAction(state) {
        var obj = {
            'Rocket': { x: 0, y: 40 },
            'Moto': { x: -40, y: 0 }
        };
        this.animation.stop();
        var _sprite = this.player.getComponent(cc.Sprite);
        _sprite.spriteFrame = cc.vv.res[state];
        this.rigidBody._b2Body.m_linearVelocity[Object.keys(obj[state])[0]] = Object.values(obj[state])[0];
        this.rigidBody._b2Body.m_linearVelocity[Object.keys(obj[state])[1]] = Object.values(obj[state])[1];

        //栅栏旁边的boost动画
        this.schedule(this.boostNode.getComponent("Boost").showBoost, 0.1);
    },

    // start () {},

    update: function update(dt) {
        //当人进入死亡状态了关闭火箭的碰撞
        if (cc.vv.isHeroDie && this.player.prop === "Rocket") {
            this.propRigidBody.enabledContactListener = false;
        }
        //boost的生成数量
        if (this.boostNode.children.length > 12) {
            this.unschedule(this.boostNode.getComponent("Boost").showBoost);
        }
        if (this.player.prop === "Moto" && this.player.x <= 1000) {
            this.rigidBody._b2Body.m_linearVelocity.x = 0;
            this.rigidBody._b2Body.m_linearVelocity.y = 10;
            this.player.prop = "Jump";
            this.player.getComponent(cc.Sprite).spriteFrame = cc.vv.res["jump"];
        }
    }
});

cc._RF.pop();